<template>
  <div class="item_container">
    <div class="item_box">
      <img src="../../assets/img/nav/a1.png" />

      <div class="info_box">
        <div class="userName_box">
          <p>{{userName}}</p>
          <!-- <p>用户名</p> -->
        </div>
        <div class="desc_box">
          <p>{{petFrom.petName}}</p>
          <!-- <p>咖啡很快就恢复健康斯大林和法律上</p> -->
        </div>
        <div class="time_box">
          <p>{{petFrom.startTime}}</p>
          <!-- <p>5345456</p> -->
          <div class="btn_box">
            <el-button size="mini"
                       @click="openDetail"
                       class="btn">详情</el-button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  props: {
    toPet: {}
  },
  created () {
    // 调用时间格式转换
    this.petFrom.startTime = this.getDateByStartDate(this.petFrom.startTime)
    // 调用获取用户名
    this.getUserNameById(this.petFrom.userId);
  },
  data () {
    return {
      petFrom: this.toPet, // 信息表单
      userName: '', // 用户名
    }
  },
  methods: {
    // 时间格式转换
    getDateByStartDate (date) {
      let dt = new Date(date)
      return dt.getFullYear() + '-' + (dt.getMonth() + 1) + '-' + dt.getDate()
    },
    // 获取用户名
    async getUserNameById (userId) {
      const { data: res } = await this.$http.get('/user/getUserNameById/' + userId)
      if (res.code == 200) {
        this.userName = res.data
        return
      }
    },
    // 将点击详情按钮传递给父组件
    openDetail () {
      this.$emit('toPetId', this.petFrom.petId);
    }
  }
}
</script>
<style lang="less" scoped>
.item_container {
  width: 100%;
  //   background-color: tomato;
  margin-top: 10px;
  border: 1px solid rgb(255, 255, 255);
  border-radius: 10px;
  background-color: rgba(238, 130, 238, 0.404);
  .item_box {
    margin: 10px;
    height: 70px;
    //  background-color: turquoise;
    display: flex;
    img {
      height: 60px;
    }
    .info_box {
      // background-color: yellowgreen;
      width: 100%;
      p {
        margin: 3px;
      }
      .userName_box {
        //   background-color: violet;
        font-size: 15px;
        font-weight: bold;
        width: 50%;
      }
      .desc_box {
        //   background-color: yellow;
        font-size: 13px;
      }
      .time_box {
        display: flex;
        font-size: 12px;
        color: rgb(63, 63, 63);
        .btn_box {
          //  background-color: blueviolet;
          width: 80%;
          position: relative;
          .btn {
            position: absolute;
            right: 0;
            transform: translate(-10%, -10%);
            background-color: violet;
            color: white;
          }
        }
      }
    }
  }
}
</style>